package com.djl.zenithbite.service.useraddress.impl;

import com.djl.zenithbite.common.response.BaseResponse;
import com.djl.zenithbite.common.threadlocal.UserContextHolder;
import com.djl.zenithbite.config.useraddress.StateEnum;
import com.djl.zenithbite.dto.request.useraddress.UserAddressAddRequest;
import com.djl.zenithbite.dto.request.useraddress.UserAddressEditRequest;
import com.djl.zenithbite.entity.UserAddress;
import com.djl.zenithbite.entity.UserAddressExample;
import com.djl.zenithbite.repository.UserAddressMapper;
import com.djl.zenithbite.service.useraddress.UserAddressService;
import com.djl.zenithbite.util.check.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Override
    public BaseResponse addUserAddress(UserAddressAddRequest request) {
        UserAddress userAddress = new UserAddress();
        userAddress.setAddress(request.getAddress());
        userAddress.setPhoneNumber(request.getPhone());
        userAddress.setIsDefault(request.getIsDefault());
        userAddress.setRecipientName(request.getUsername());
        userAddress.setUserId(UserContextHolder.getUser().getId());
        userAddress.setCreatedAt(new Date());
        userAddress.setUpdatedAt(new Date());
        userAddress.setCity("");
        userAddress.setAddressType("");
        userAddress.setState(StateEnum.ACTIVE.getCode());

        if (request.getIsDefault()){
            updateIsDefault(false,UserContextHolder.getUser().getId());
        }

        userAddressMapper.insertSelective(userAddress);

        BaseResponse<UserAddress> baseResponse = new BaseResponse<>();
        baseResponse.setCode(BaseResponse.SUCCESS);
        baseResponse.setData(userAddress);
        return baseResponse;
    }

    @Override
    public BaseResponse editUserAddress(UserAddressEditRequest request) {

        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(request.getId());
        AssertUtil.notNull(userAddress,"user address not exists");

        AssertUtil.isTrue(Objects.equals(UserContextHolder.getUserPrimaryId(), userAddress.getUserId()),"this address is not belong you,can not edit");

        if (request.getIsDefault()){
            updateIsDefault(false,UserContextHolder.getUser().getId());
        }

        userAddress.setUpdatedAt(new Date());
        userAddress.setAddress(request.getAddress());
        userAddress.setPhoneNumber(request.getPhone());
        userAddress.setIsDefault(request.getIsDefault());
        userAddress.setRecipientName(request.getUsername());

        int i = userAddressMapper.updateByPrimaryKeySelective(userAddress);

        BaseResponse<Integer> baseResponse = new BaseResponse<>();
        baseResponse.setCode(BaseResponse.SUCCESS);
        baseResponse.setData(i);

        return baseResponse;
    }

    private void updateIsDefault(Boolean isDefault,int userId){
        UserAddressExample userAddressExample = new UserAddressExample();
        UserAddressExample.Criteria criteria = userAddressExample.createCriteria();
        criteria.andUserIdEqualTo(userId);

        UserAddress userAddress = new UserAddress();
        userAddress.setIsDefault(isDefault);

        userAddressMapper.updateByExampleSelective(userAddress,userAddressExample);
    }

    @Override
    public BaseResponse queryUserAddress() {
        UserAddressExample example = new UserAddressExample();
        UserAddressExample.Criteria criteria = example.createCriteria();
        criteria.andStateEqualTo(StateEnum.ACTIVE.getCode());
        criteria.andUserIdEqualTo(UserContextHolder.getUserPrimaryId());
        List<UserAddress> userAddresses = userAddressMapper.selectByExample(example);

        BaseResponse<List<UserAddress>> baseResponse = new BaseResponse<>();
        baseResponse.setCode(BaseResponse.SUCCESS);
        baseResponse.setData(userAddresses);

        return baseResponse;
    }

    @Override
    public BaseResponse deleteUserAddress(int id) {

        UserAddress userAddress = userAddressMapper.selectByPrimaryKey(id);
        AssertUtil.notNull(userAddress,"user address not exists");

        userAddress.setState(StateEnum.DELETE.getCode());

        int i = userAddressMapper.updateByPrimaryKey(userAddress);

        BaseResponse<Integer> baseResponse = new BaseResponse<>();
        baseResponse.setCode(BaseResponse.SUCCESS);
        baseResponse.setData(i);

        return baseResponse;
    }
}
